package com.bw.live.widget

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.imageloader.ImageLoaderManager
import com.bw.live.R
import com.bw.live.databinding.DialogGiftBinding
import com.bw.live.databinding.ItemGiftBinding
import com.bw.live.model.entity.GiftEntity
import com.google.android.material.bottomsheet.BottomSheetDialog

/**
 * @author:created by hahajing
 * 时间：2024/5/10 0010
 * 企鹅号:444511958
 * 描述:
 */
class GiftBottomDialog:BottomSheetDialog {
    private lateinit var binding: DialogGiftBinding
    private lateinit var adapter: GiftAdapter
    constructor(context: Context):super(context){
        initView(context)
        initData()
        initEvent()
    }

    private fun initData() {
        val giftArrayRoot = context.resources.getStringArray(R.array.giftarray)
        val giftArrayNormal = context.resources.getStringArray(R.array.normal_giftarray)

        val gifts:MutableList<GiftEntity> = mutableListOf()
        giftArrayRoot.forEach {
            val entity = GiftEntity(txt = it)
            gifts.add(entity)
        }
        giftArrayNormal.forEach {
            val entity = GiftEntity(txt = it)
            gifts.add(entity)
        }
        adapter.refresh(gifts)

    }
    /**
     * 选中的监听
     */
    var selectedListener:(GiftEntity)->Unit={}

    /**
     * 赠送监听
     */
    var sendListener:()->Unit = {}
    private fun initView(context:Context) {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_gift,null,false)
        binding = DialogGiftBinding.bind(view)
        setContentView(binding.root)

        binding.rvDialogGift.layoutManager = GridLayoutManager(context,5)
        adapter= GiftAdapter()
        adapter.selectedListener={
            selectedListener.invoke(it)
        }
        binding.rvDialogGift.adapter=adapter
    }

    private fun initEvent() {
        binding.btnDialogGiftSend.setOnClickListener {
            sendListener.invoke()
        }
    }


    class GiftAdapter:RecyclerView.Adapter<GiftAdapter.ViewHolder>(){
        private var gifts:MutableList<GiftEntity>  = mutableListOf()
        private lateinit var mContext: Context

        /**
         * 选中的监听
         */
        var selectedListener:(GiftEntity)->Unit={}

        fun refresh(_gifts:MutableList<GiftEntity>){
            this.gifts=_gifts
            notifyDataSetChanged()
        }

        class ViewHolder(view:View):RecyclerView.ViewHolder(view){
            val binding:ItemGiftBinding by lazy {
                ItemGiftBinding.bind(view)
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            mContext=parent.context
            val view =LayoutInflater.from(parent.context).inflate(R.layout.item_gift,parent,false)
            return ViewHolder(view)
        }

        override fun getItemCount(): Int {
            return gifts.size
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val entity = gifts.get(position)
            ImageLoaderManager.getInstance().loadFileIntoImageView(mContext, com.bw.common.R.mipmap.ic_launcher,holder.binding.ivItemGiftImg,null)
            holder.binding.tvItemGiftTxt.text=entity.txt
            holder.binding.llItemGift.tag = entity
            holder.binding.llItemGift.setOnClickListener(listener)
            if (entity.isSelected){
                holder.binding.llItemGift.background = mContext.resources.getDrawable(R.drawable.gift_selected)
            }else{
                holder.binding.llItemGift.background = mContext.resources.getDrawable(R.drawable.gift_normal)
            }
        }

        private val listener:View.OnClickListener = object :View.OnClickListener{
            override fun onClick(v: View?) {
                gifts.forEach {
                    it.isSelected=false
                }
                val entity:GiftEntity = v?.tag as GiftEntity
                entity.isSelected = true

                selectedListener.invoke(entity)
                notifyDataSetChanged()
            }

        }
    }

}